/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.message;

import hextd.message.MessageFactory.NetworkMessage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageReplayLogSP implements MessageReplayLog {

	public NetworkMessage type = NetworkMessage.ReplayLogSP;
	// Level Score MapId Replay Public Username
	public static String RegExp = "REPLAYLOGSP ([0-9]+) ([0-9]+) ([-0-9]+) ([^\\n]+) ([a-z]+) ([^\\n]+)";
	public static Pattern MessagePattern = Pattern.compile(MessageReplayLogSP.RegExp);
	//
	private String message;
	private String log;
	private int level;
	private long score;
	private int mapId;
	private Boolean isPublic;
	private String userName = "X";
	//
	private int replayId = 0;
	private int userId = 0;

	public static String formatMessage(int level, long score, int mapId, String log, Boolean isPublic, String userName) {
		String message;
		try {
			message = "REPLAYLOGSP " + level +
					" " + score +
					" " + mapId +
					" " + URLEncoder.encode(log, "UTF-8") +
					" " + isPublic.toString() +
					" " + URLEncoder.encode(userName, "UTF-8");
		} catch (UnsupportedEncodingException ex) {
			Logger.getLogger(MessageReplayLogSP.class.getName()).log(Level.SEVERE, null, ex);
			message = "";
		}
		return message;
	}

	public MessageReplayLogSP() {
	}

	private MessageReplayLogSP(String message) {
		this.message = message;
		Matcher matcher = MessageReplayLogSP.MessagePattern.matcher(message);
		if (matcher.matches()) {
			try {
				level = Integer.parseInt(matcher.group(1));
				score = Long.parseLong(matcher.group(2));
				mapId = Integer.parseInt(matcher.group(3));
				String encodedLog = matcher.group(4);
				log = URLDecoder.decode(encodedLog, "UTF-8");
				isPublic = Boolean.parseBoolean(matcher.group(5));
				userName = URLDecoder.decode(matcher.group(6), "UTF-8");
			} catch (NumberFormatException e) {
				System.err.println("MessageSay: invalid id in: " + matcher.group(1));
			} catch (UnsupportedEncodingException e) {
				Logger.getLogger(MessageReplayLogSP.class.getName()).log(Level.SEVERE, null, e);
			}
		}
	}

	public MessageReplayLogSP(int level, long score, int mapId, String log, Boolean isPublic, String userName) {
		this.log = log;
		this.level = level;
		this.score = score;
		this.mapId = mapId;
		this.isPublic = isPublic;
		this.userName = userName;
	}

	@Override
	public String getMessage() {
		if (message == null) {
			message = formatMessage(level, score, mapId, log, isPublic, userName);
		}
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		return new MessageReplayLogSP(message);
	}

	@Override
	public int getSenderId() {
		return -2;
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Pattern getPattern() {
		return MessageReplayLogSP.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}

	@Override
	public String getLog() {
		return log;
	}

	public int getLevel() {
		return level;
	}

	public long getScore() {
		return score;
	}

	@Override
	public int getMapId() {
		return mapId;
	}

	public boolean isPublic() {
		return isPublic;
	}

	public void setPublic(boolean isPublic) {
		isPublic = new Boolean(isPublic);
		message = null;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
		message = null;
	}

	@Override
	public int getReplayId() {
		return replayId;
	}

	public void setReplayId(int replayId) {
		this.replayId = replayId;
		message = null;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
		message = null;
	}
}
